package com.boruishangge.aplus_app.activity

import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.boruishangge.common.APlusARoute
import com.boruishangge.common.view.TabbaHomeView
import com.boruishangge.home.fragment.HomeFragment
import com.boruishangge.aplus_app.R
import com.boruishangge.mine.fragment.MineFragment
import com.boruishangge.order.fragment.OrderListFragment
import com.boruishangge.persagysdk.base.BaseActivity
import kotlinx.android.synthetic.main.activity_index_view.*

@Route(path = APlusARoute.index_activity_Router)
class IndexActivity:BaseActivity(),View.OnClickListener,TabbaHomeView.OnCheckedChangeListener {

    companion object {
        val PERMISSION_TAKE_PICTURE = arrayOf("android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE")
        private val REQUEST_CODE_QRCODE_PERMISSIONS = 1
        private val fragments = arrayOf<Fragment>(HomeFragment.getInstance(),OrderListFragment.getInstance(),MineFragment.getInstance()   )
    }
    var currentPosition = -1
    val that=this@IndexActivity
    override fun onClick(v: View?) {

    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_index_view)
        initView()
        tabbar_view.setOnCheckedChangeListener(this)
    }

      fun initView() {
        currentPosition = 0
        selectTabbar(currentPosition)
    }

    override fun onCheckedChanged(index: Int) {
        currentPosition = index
        selectTabbar(currentPosition)
    }

    fun selectMemberTabbar(pos: Int) {
         tabbar_view.setSelectTab(pos)
    }
    private fun selectTabbar(index: Int) {
        val ft = supportFragmentManager.beginTransaction()
        val currentFragment = fragments[index]
        if (!currentFragment.isAdded) {
            ft.add(lly_content .id, currentFragment)
        }
        for (i in fragments.indices) {
            if (index == i) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    if (i == 0) {
                        whiteStatusBar()
                    } else if (i == 1) {
                        whiteStatusBar()
                    } else if (i == 2) {
                        whiteStatusBar()
                    }
                }
                ft.show(fragments[i])
            } else {
                ft.hide(fragments[i])
            }
        }
        ft.commitAllowingStateLoss()
    }



}